package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServicelmpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /*
     * 添加购物车
     * */
    @Override
    public void addShopingCatr(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入购物车的商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果已经存在，则只需要把数量加一
        if (list != null && list.size() > 0) {
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            //此时再更新数据库中的number字段
            shoppingCartMapper.updateNumberbyId(shoppingCart1);
        } else {
            //如果不存在，就执行插入数据

            //分别获取dishid和setmealid，以此来判断此时的商品是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();

            if (dishId != null) {
                //本次添加到购物车的是菜品
                Dish dish = dishMapper.getByid(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());


            } else {
                //本次添加到购物车的是套餐
                Long setmealId = shoppingCart.getSetmealId();

                Setmeal setmeal = setmealMapper.getByid(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());


            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);

        }


    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        List<ShoppingCart> list= shoppingCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void cleanAllByid(Long currentId) {
        shoppingCartMapper.clean(currentId);

    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //先确定删除的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null)
        {
            //删除的是菜品
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
            ShoppingCart shoppingCart1 = list.get(0);
            //判断当前删除是否只有一份
            if(shoppingCart1.getNumber()==1)
            {
                shoppingCartMapper.deleteByid(shoppingCart1.getDishId());
            }
            else{
                shoppingCart1.setNumber(shoppingCart1.getNumber()-1);

                shoppingCartMapper.updateNumberbyId(shoppingCart1);

            }
        }
        else {
            //删除的是套餐
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
            ShoppingCart shoppingCart1 = list.get(0);
            //判断当前删除是否只有一份
            if(shoppingCart1.getNumber()==1)
            {
                shoppingCartMapper.deleteByid(shoppingCart1.getSetmealId());
            }
            else{
                shoppingCart1.setNumber(shoppingCart1.getNumber()-1);

                shoppingCartMapper.updateNumberbyId(shoppingCart1);

            }
        }
    }


}
